/*
 *   Clovis
 *   Copyright (C) 2008 SFWOL
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef __XML_FILE_READER_H
#define __XML_FILE_READER_H

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>

#include "../core/Tree.h"
#include "../core/CString.h"
#include "../core/CInteger.h"

namespace clovis {

	namespace datas {

		/*!
		 * XMLFileReader creates a Tree from an external XML file.
		 */
		class XMLFileReader {

			private:

				core::Tree * tree;			//! The built tree

				core::CInteger tags;		//! The number of tags in the document (<foo></foo>)
				core::CInteger single_tags;	//! The number of single tags in the document (<foo/>)
				core::CInteger attributes;	//! The number of atributes in the document
				core::CInteger max_depth;	//! The maximum depth found in the document
				core::CInteger errors;		//! The number of errors encountered, parsing the document
				core::CInteger warning;		//! The number of warnings encountered, parsing the document
				core::CString file;			//! The name of the current file

				core::CInteger current_depth;	//! The current depth while parsing the document
				core::CInteger current_line;	//! The current line of the parsed file
				std::vector<core::CString> ariane;//! A list of all the unclosed nodes (is used to check integrity of the XML tree)

				/*!
				 * Parse a line and modify the tree in consequence.
				 * \arg line The line to parse
				 */
				void parseLine(core::CString line);

				/*!
				 * Parse a tag and modify the tree in consequence.
				 * \arg tag The tag to parse
				 */
				void parseAttributes(core::CString tag);

				/*!
				 * Return the name of the tag passed in argument
				 * \arg tag the raw tag
				 * \return The name extracted from the raw tag
				 */
				core::CString getTagName(core::CString tag);

			public:

				/*!
				 * Create a new Tree from an external file.
				 * \arg input_file The file who will be parsed.
				 */
				XMLFileReader(core::CString input_file);

				/*!
				 * Get the constructed tree.
				 * \todo TODO !!!
				 */
				void getTree();

				/*!
				 * Print the parser's report on the standard output.
				 * The berbose options show more informations about the tags, errors and warnings.
				 * \arg verbose Active (or no) the verbose mode.
				 */
				void showReport(bool verbose = false);

		};

	};

};

#endif
